<template>
       <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
         <el-button type="primary" icon="Plus" @click="onopen">添加品牌</el-button>
      </div>
        <el-table :data="tableData" style="width: 100%">
             <el-table-column type="index"  label="序号"  width='100px'/>
    <el-table-column prop="tmName" label="产品名称"/>

 
    <el-table-column  label="LOGO"  >
    <template #="{ row}" >
   <img :src="row.logoUrl"  style="width:100px;height: 100px;">
   </template>
   </el-table-column>
    <!-- <img :src="tableData.logoUrl"> -->
    <el-table-column  label="品牌操作" >
       <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
         <el-popconfirm title="确定删除吗?"
          @confirm="handleDelete(scope.$index, scope.row)">
    <template #reference>
        <el-button
          size="small"
          type="danger"
         
        >
          删除
        </el-button>
    </template>
  </el-popconfirm>
      </template>
        </el-table-column>
  </el-table>
  
    </template>
    <div class="demo-pagination-block">
 
    <el-pagination
    
     
      :page-sizes="[3, 10, 15, 20]"
      :size="size"
      :default-page-size="3"
      layout=" sizes, prev, pager, next, ->,jumper,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  </el-card>
   <el-dialog
    v-model="dialogVisible"
    :title="trademarkParams.tmName?'编辑':'添加'"
     :before-close="cancel"
 
    width="500"
    
  >
    <el-form style="width: 80%;" :model="trademarkParams"  ref="formRef">
                <el-form-item label="品牌名称" label-width="100px" prop="tmName">
                    <el-input placeholder="请您输入品牌名称" v-model="trademarkParams.tmName"></el-input>
                </el-form-item>
                <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
                  <el-upload
    class="avatar-uploader"
    action="/api/admin/product/fileUpload"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="trademarkParams.logoUrl" :src="trademarkParams.logoUrl" class="avatar" style="width: 100px;height: 100px;"/>
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
                  </el-form-item>
                  </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button  @click="cancel">Cancel</el-button>
        <el-button type="primary" @click="isok">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { onMounted, ref,reactive } from 'vue'
import {reqHasTrademark,reqDeleteTrademark } from "@/api/product/trademark/index";
import { ComponentSize, ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'
const tableData=ref([])
const formRef=ref()
const total=ref()
const dialogVisible=ref(false)
const trademarkParams=ref({
    tmName: '',
    logoUrl: ''
})
onMounted(()=>{
getHasTrademark()
})
const getHasTrademark=async(pager=1)=>{
  pageNo.value =pager
    const res= await reqHasTrademark(pageNo.value,limit.value)
 tableData.value=res.data.records
 total.value=res.data.total
    console.log( tableData.value)

}

const pageNo=ref(1)
const limit=ref(3)
const size = ref<ComponentSize>('default')
const onopen=()=>{
  dialogVisible.value=true
  console.log(formRef.value)

}

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
  limit.value=val
  getHasTrademark()
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  pageNo.value=val
  getHasTrademark()
}
const handleEdit = (index: number, row:any) => {
  console.log(index, row)
  dialogVisible.value=true
  
  Object.assign(trademarkParams.value,row)

}
const cancel=()=>{
  dialogVisible.value=false,
  trademarkParams.value.tmName=''
  trademarkParams.value.logoUrl=''
  
}
const handleDelete =async (index: number, row:any) => {
  console.log(index, row)
  let result = await reqDeleteTrademark(row.id);
    if (result.code == 200) {
        //删除成功提示信息
        ElMessage({
            type: 'success',
            message: '删除品牌成功'
        });
        //再次获取已有的品牌数据
        getHasTrademark(tableData.value.length > 1 ? pageNo.value : pageNo.value - 1);
    } else {
        ElMessage({
            type: 'error',
            message: '删除品牌失败'
        })
    }
}
const isok=()=>{

  dialogVisible.value=false
}

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
 
  uploadFile
) => {
   console.log(uploadFile)
  trademarkParams.value.logoUrl = response.data
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
</script>
<style scoped lang="scss">
.card-header{
    width: 100%;
}
.demo-pagination-block {
text-align: center;
}
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #1067f4;
  border: 1px solid rgb(67, 67, 67);
  width: 178px;
  height: 178px;
  text-align: center;
}
.el-scrollbar {
  .el-scrollbar__bar.is-horizontal {
    height: 14px;
  }
  .el-scrollbar__bar.is-vertical {
    width: 14px;
  }
  .el-scrollbar__bar.is-horizontal .el-scrollbar__thumb {
    opacity: 1;
    background-color: rgb(231, 227, 4);
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.15);
  }

  .el-scrollbar__bar.is-vertical .el-scrollbar__thumb {
    opacity: 1;
    background-color: rgb(228, 214, 19);
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.15);
  }
}

</style>